//将两个字节的byte数组变成整数
var bytes2int2 = function(b){
	return ( (b[0] & 0xFF) << 8 | b[1] & 0xFF) ;
};

//int类型转换成一个2长度的byte数组
var int2bytes2 = function(num){
	return [ (num>>8)&0xFF , num&0xFF ];
};

//int类型转换成一个4长度的byte数组
var int2bytes4 = function(num){
	return [ (num>>24)&0xff, (num>>16)&0xff, (num>>8)&0xff, (num)&0xff ];
};

//将四个字节的byte数组变成整数
var bytes2int4 = function(b){
	return ( (b[0] & 0xFF) << 24 | (b[1] & 0xFF) << 16 | (b[2] & 0xFF) << 8 | b[3] & 0xFF ) ;
};

//BCD码转为10进制串
var bcd2Str = function(b){
	var res = '';
	for(var i=0; i<b.length; i++){
		res += (b[i] & 0xF0) >>> 4;
		res += b[i] & 0xF;
	}
	res = res.slice(0,1) == '0' ? res.substring(1) : res ;//处理手机号的首位0
	return res;
};

//时间字符串yyMMddHHmmss转Date
var str2Date = function(str){
	var s = '20'+str.substring(0,2)+'/'+str.substring(2,4)+'/'+str.substring(4,6)+' '
	+str.substring(6,8)+':'+str.substring(8,10)+':'+str.substring(10);
	return new Date(s);
};

//时间Date转字符串yyMMddHHmmss
var Date2str;
Date2str = function (date) {
	var res = '';
	res += date.getFullYear() % 100;
	res += parseInt((date.getMonth() + 1) / 10) + '' + (date.getMonth() + 1) % 10;
	res += parseInt(date.getDate() / 10) + '' + date.getDate() % 10;
	res += parseInt(date.getHours() / 10) + '' + date.getHours() % 10;
	res += parseInt(date.getMinutes() / 10) + '' + date.getMinutes() % 10;
	res += parseInt(date.getSeconds() / 10) + '' + date.getSeconds() % 10;
	return res;
};

// String 转换成BCD数组
var str2Bcd = function(str){
	if( (str.length%2) != 0){
		str = '0' + str;
	}
	var buf = new Buffer(str);
	var resLength = buf.length/2;
	var m=0,n=0,res=[];
	for(var i=0; i<resLength; i++){
		var front = buf[2*i];
		var back = buf[2*i+1];
		if(front>=48 && front<=57){
			m = front-48;
		}else if(front>=97 && front<=112){
			m = front-97+0x0a;
		}else{
			m = front-65+0x0a;
		}
		if(back>=48 && back<=57){
			n = back-48;
		}else if(back>=97 && back<=112){
			n = back-97+0x0a;
		}else{
			n = back-65+0x0a;
		}
		res[i] = (m<<4)+n;
	}
	return res;
};

//String转unicode
var str2unicode = function(str){
	var res = [];
	for(var i=0;i<str.length;i++){
		res.push(str.charCodeAt(i));
	}
	return res;
};

//按位校验
var checkBytes = function(buf){
	var res = buf[0]^buf[1];
	for(var i=2;i<(buf.length-1);i++){
		res ^= buf[i];
	}
	if (res != buf[buf.length - 1]) {
		return false;
	} else {
		return true;
	}
};

//生成校验位
var makeCheckByte = function(buf){
    var res = buf[0]^buf[1];
    for(var i=2;i<(buf.length);i++){
        res ^= buf[i];
    }
    return res;
};

//躲避7e和7d
var escapeData = function(buf){
	var res = [];
	for(var i=0;i<buf.length;i++){
		if(buf[i] == 126){
			res.push(125);
			res.push(2);
		}else if(buf[i] == 125){
			res.push(125);
			res.push(1);
		}else{
			res.push(buf[i]);
		}
	}
	return res;
};

//终端通用应答完整消息 msgid:消息ID, tphone:终端手机号, msgSNum:消息流水号, resSNum:应答流水号, resId:应答ID, resmsg:应答结果
var generalResponse = function(msgid, tphone, msgSNum, resSNum, resId, resmsg){
	var main = [].concat(resSNum,resId,resmsg);
	return makeMessage(msgid, tphone, msgSNum, main);
};
//生成16进制字符串信息
var makeMessage = function(msgid, tphone, msgSNum, mainMsg){
	var body = [].concat(msgid,int2bytes2(mainMsg.length),tphone,msgSNum,mainMsg);
	body.push(makeCheckByte(body));
	var all = escapeData(body);
	var buffer = [].concat(0x7e, all, 0x7e);

    var str = '';
    for (var i = 0; i < buffer.length; i++) {
        if (buffer[i] < 16) {
            str += '0' + buffer[i].toString(16);
        } else {
            str += buffer[i].toString(16);
        }
    }
    return str;
};
//生成16进制字数组信息
var makeByteMessage = function(msgid, tphone, msgSNum, mainMsg){
	var body = [].concat(msgid,int2bytes2(mainMsg.length),tphone,msgSNum,mainMsg);
	body.push(makeCheckByte(body));
	var all = escapeData(body);
	var buffer;
	buffer = [].concat(0x7e, all, 0x7e);
	return buffer;
};

//自增长值
var serNumber = 0;
var getSerialNumber = function(){
	if(serNumber == 65535){
		serNumber = 1;
	}else{
		serNumber ++;
	}
	return int2bytes2(serNumber);
};
//int类型转换成一个4字节长度的byte数组
var intToByteArray = function(value){
	var b = [];
	for(var i=0;i<4;i++){
		var offset = (3 - i) * 8;
		b[i] = value >>> offset & 0xFF;
	}
	return b;
};
//16精子字符串转btye
var hexstr2btyes = function(str){
	if( (str.length%2) != 0){
		str = '0' + str;
	}
	var res = [];
	for(var i=0;i<(str.length/2);i++){
		res[i] = parseInt('0x'+str.substring(i*2,i*2+2));
	}
	return res;
};

module.exports = {
	checkBytes: checkBytes,
	escapeData: escapeData,
	bytes2int2: bytes2int2,
	int2bytes2: int2bytes2,
	int2bytes4: int2bytes4,
	bytes2int4: bytes2int4,
	makeMessage: makeMessage,
	makeByteMessage:makeByteMessage,
	generalResponse: generalResponse,
	getSerialNumber: getSerialNumber,
	bcd2Str: bcd2Str,
	str2Bcd: str2Bcd,
	str2unicode: str2unicode,
	str2Date: str2Date,
	Date2str: Date2str,
	hexstr2btyes: hexstr2btyes,
	intToByteArray: intToByteArray
};